import React from 'react';
import { Input, Form, Modal,Button } from 'antd';
import _ from 'lodash';
const FormItem = Form.Item;

import { changePage,_validate } from '../store/actions';

class FormEditDataDictionary extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            visible:this.props.visible || false,
            title: this.props.title || '编辑数据项',
            targetData:this.props.targetData || {}
        };
        this.onOk = this.onOk.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.resetFields = this.resetFields.bind(this);
    }
    resetFields(){
        this.props.form.resetFields()
    }
    static contextTypes = {
        router:React.PropTypes.object
    };
    onOk() {
        const _this = this;
        _validate(null,_this,function(data){
            console.log('提交的数据',data)
        })
    }
    onCancel(){
        this.props.onCancel();
        this.resetFields()
    }
    setInitialValue(data){
        const {setFieldsValue} = this.props.form;
        setFieldsValue(data)
    }
    componentWillReceiveProps(nextProp){
        this.setState({
            visible:nextProp.visible,
            title:nextProp.title,
            targetData:nextProp.targetData
        })
    }
    componentDidMount(){
        this.setInitialValue(this.state.targetData)
    }
    render() {
        const { getFieldProps } = this.props.form;
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 16 }
        };

        const dataDictionaryName = getFieldProps('name', {
            rules: [
                { required: true, message: '请输入数据项名称'}
            ],
            initialValue: this.state.targetData && this.state.targetData.name
        });
        const dataDictionaryDesc = getFieldProps('desc', {
            rules: [
                { required: false, message: '请输入数据项描述'}
            ],
            initialValue: this.state.targetData && this.state.targetData.desc

        });
        const dataDictionaryCode = getFieldProps('code', {
            rules: [
                { required: true, message: '请输入数据项序号'}
            ],
            initialValue: this.state.targetData && this.state.targetData.code
        });
        return (
            <Modal
                visible = {this.state.visible}
                title = {this.state.title}
                confirmLoading = {this.state.confirmLoading}
                onOk = {this.onOk}
                onCancel = {this.onCancel}
            >
                <Form horizontal form={this.props.form}>
                    <FormItem
                        {...formItemLayout}
                        label="名称"
                    >
                        <Input
                            {...dataDictionaryName}
                            placeholder="请输入数据项名称"
                        />
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="序号"
                    >
                        <Input
                            {...dataDictionaryCode}
                            placeholder="请输入数据项序号"
                        />
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="描述"
                    >
                        <Input
                            {...dataDictionaryDesc}
                            type="textarea"
                            placeholder="请输入数据项描述"
                        />
                    </FormItem>
                </Form>
            </Modal>
        )
    }

}

export default FormEditDataDictionary = Form.create()(FormEditDataDictionary);